#include <stdio.h>
#include <stdlib.h>

// 定义链表节点结构
struct Node {
    int data;
    struct Node* next;
};
// 创建新节点
struct Node* createNode(int data) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    if (newNode == NULL) {
        printf("Memory allocation failed.\n");
        exit(1);
    }
    newNode->data = data;
    newNode->next = NULL;
    return newNode;
}
// 在链表末尾插入节点
void insertAtEnd(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    
    if (*head == NULL) {
        *head = newNode;
        return;
    }
    struct Node* current = *head;
    while (current->next != NULL) {
        current = current->next;
    }
    current->next = newNode;
}
// 在链表头部插入节点
void insertAtBeginning(struct Node** head, int data) {
    struct Node* newNode = createNode(data);
    
    newNode->next = *head;
    *head = newNode;
}
// 在指定位置插入节点
void insertAtPosition(struct Node** head, int data, int position) {
    struct Node* newNode = createNode(data);
    
    if (position <= 0) {
        printf("Invalid position.\n");
        return;
    }
    
    if (position == 1) {
        newNode->next = *head;
        *head = newNode;
        return;
    }
    
    struct Node* current = *head;
    int count = 1;
    while (count < position - 1 && current != NULL) {
        current = current->next;
        count++;
    }
    
    if (current == NULL) {
        printf("Invalid position.\n");
        return;
    }
    
    newNode->next = current->next;
    current->next = newNode;
}

// 从链表中删除指定元素
void deleteNode(struct Node** head, int data) {
    struct Node* temp = *head;
    struct Node* prev = NULL;

    if (temp != NULL && temp->data == data) {
        *head = temp->next;
        free(temp);
        return;
    }

    while (temp != NULL && temp->data != data) {
        prev = temp;
        temp = temp->next;
    }

    if (temp == NULL) {
        printf("Element not found.\n");
        return;
    }

    prev->next = temp->next;
    free(temp);
}

// 遍历并打印链表的元素
void printLinkedList(struct Node* head) {
    if (head == NULL) {
        printf("Linked list is empty.\n");
        return;
    }
    
    printf("Linked list elements: ");
    struct Node* current = head;
    while (current != NULL) {
        printf("%d ", current->data);
        current = current->next;
    }
    printf("\n");
}

// 删除整个链表
void deleteLinkedList(struct Node** head) {
    struct Node* current = *head;
    struct Node* next;
    
    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }
    
    *head = NULL;
}

int main() {
    struct Node* head = NULL;
    
    insertAtEnd(&head, 1);
    insertAtEnd(&head, 2);
    insertAtEnd(&head, 3);
    
    printLinkedList(head); // 1 2 3
    
    insertAtBeginning(&head, 4);
    
    printLinkedList(head); // 4 1 2 3
    
    insertAtPosition(&head, 5, 3);
    
    printLinkedList(head); // 4 1 5 2 3
    
    deleteNode(&head, 2);
    
    printLinkedList(head); // 4 1 5 3
    
    deleteLinkedList(&head);
    
    printLinkedList(head); // Linked list is empty.
    
    return 0;
}



